package ai.mspbots.test.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createDate", LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, "updateDate", LocalDateTime::now, LocalDateTime.class);
        // 这里可以设置默认的创建人和更新人，比如从SecurityContext中获取当前用户ID
        this.strictInsertFill(metaObject, "creator", () -> getCurrentUserId(), Long.class);
        this.strictInsertFill(metaObject, "updater", () -> getCurrentUserId(), Long.class);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateDate", LocalDateTime::now, LocalDateTime.class);
        this.strictUpdateFill(metaObject, "updater", () -> getCurrentUserId(), Long.class);
    }

    private Long getCurrentUserId() {
        // 实现获取当前用户ID的逻辑
        // 例如：return SecurityUtils.getCurrentUserId();
        return 1L; // 临时默认值
    }
}